package ledrpt

import org.kdx.DateUtils

import java.text.SimpleDateFormat

class WipController {

    def dynamicQueryService
    //ͼ������
    def wipfollow(){
        params.SYS_QUERY_NAME='wipfollowlist'
        def wipfollowlist =dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME='wipfollowlist2'
        def wipfollowlist2 =dynamicQueryService.queryForList(params)
        params.SYS_QUERY_NAME = 'packageForm1'
        def packageForm1 = dynamicQueryService.queryForList(params)


        [wipfollowlist:wipfollowlist,wipfollowlist2:wipfollowlist2,packageForm1:packageForm1]
    }
    def wipList(){
        SimpleDateFormat sdf = new SimpleDateFormat('yyyy-MM-dd HH:mm:ss')
        def startTime = params.startTime ? sdf.parse(params.startTime) : sdf.parse(sdf.format(new Date().clearTime()-30))
        def endTime = params.endTime ? sdf.parse(params.endTime) : sdf.parse(sdf.format(new Date()))
        params.startTime = sdf.format(startTime)
        params.endTime = sdf.format(endTime)
        def dayList = DateUtils.getHourList(startTime, endTime)
        params.SYS_QUERY_NAME='wipPackage'
        def wipPackage=dynamicQueryService.queryForList(params)
        def wipList
        if (params.wipTime == null || params.wipTime == "") {
            params.SYS_QUERY_NAME = 'wipList'
            wipList = dynamicQueryService.queryForList(params)
        } else {
            params.SYS_QUERY_NAME = 'stepWip7h'
            wipList = dynamicQueryService.queryForList(params)
        }

        def sumNum=0

        for(def i=0;i<wipList.size();i++){
            sumNum+=wipList[i].MAIN_QTY
        }

        [startTime:startTime,endTime:endTime,wipList:wipList,wipPackage:wipPackage,wipTime:params.wipTime,sumNum:sumNum,part_name:params.part_name,SUBSTRATE_ID1:params.SUBSTRATE_ID1,
         LOT_ID:params.LOT_ID,RESERVED1:params.RESERVED1,CUSTOMER_ORDER:params.CUSTOMER_ORDER,STEP_NAME:params.STEP_NAME]
    }

    def wipfollowlist(){
        params.SYS_QUERY_NAME='wipfollowlist1'
        def wipfollowlist =dynamicQueryService.queryForList(params)

        [wipfollowlist:wipfollowlist]
    }

    def exportExcel() {
        def sysQueryName
        def tab = params.timeType// ?:'H'
        //��?
        if(tab.equals('H')) {
            sysQueryName = params.SYS_QUERY_NAME+'HList'
        }else if (tab.equals('D')){
            sysQueryName = params.SYS_QUERY_NAME+'DList'
        }else if (tab.equals('W')){
            sysQueryName = params.SYS_QUERY_NAME+'WList'
        }else if (tab.equals('M')){
            sysQueryName = params.SYS_QUERY_NAME+'MList'
        }else if (tab.equals('Q')){
            sysQueryName = params.SYS_QUERY_NAME+'QList'
        }else if (tab.equals('Y')){
            sysQueryName = params.SYS_QUERY_NAME+'YList'
        }else{
            sysQueryName = params.SYS_QUERY_NAME
        }
        params.startNo = 1
        params.endNo = 99999999999999999
        params.startno = 1
        params.endno = 99999999999999999
        params.SYS_QUERY_NAME = sysQueryName

        def dataList = dynamicQueryService.queryForList(params)

        def tableHeadList =  []
        if(dataList.size()>0){
            tableHeadList =  dataList[0].keySet()
        }
        String filename = new String("${sysQueryName}.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "export${params.EXPORT_CONTENT_NAME}", model: [dataList: dataList,tableHeadList:tableHeadList])
    }
    //�޸�finaly��
    def updatewip(DwsWip c){
        render (view:'updatewip',
                model:[wip:c])


    }
    //�����޸�finaly�����Ϣ
    def addwip(DwsWip cfu){
        def c=DwsWip.get(106)
        print c.id
        cfu.save()
        render """{
                    "statusCode":"200",
                    "message":"${message(code: 'default.created.message', args: [message(code: 'DwsWip.label', default: 'DwsWip'), cfu.id])}",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""
    }
    //ɾ��defect����Ϣ
    def deletewip(DwsWip c){

        c.delete()
        render """{
                    "statusCode":"200",
                    "message":"${message(code: 'default.created.message', args: [message(code: 'DwsWip.label', default: 'DwsWip'), c.id])}",
                    "closeCurrent":true,
                    "forward":"",
                    "forwardConfirm":""
                    }"""

    }

    def exportExce2() {


        def dataList = []

        def tableHeadList =  []

        String filename = new String("WipExcel.xls".getBytes("UTF-8"), "UTF-8");
        response.setContentType('application/vnd.ms-excel;charset=UTF-8')
        response.setHeader('Content-disposition', "attachment;filename=${filename};charset=UTF-8")
        response.setCharacterEncoding("UTF-8");
        render(template: "exportExcel", model: [dataList: dataList,tableHeadList:tableHeadList])
    }
}
